//
//  AdiumStatz.m
//  Statz
//
//  Created by dmaclach on 6/17/07.
//
// Copyright 2007 Google Inc.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
// the License.
//

#import "AdiumStatz.h"
#import "AEVTBuilder.h"
#import "StatusUpdate.h"

@implementation AdiumStatz
- (NSString*)appBundleID {
  return @"com.adiumX.adiumX";
}

- (NSString*)uuid {
  return @"18E1D504-E76C-486E-8592-C96D2FFACAE7";
}

- (StatzUpdateValue)updateStatz:(StatusUpdate*)sender {
  NSString *newValue = [sender status];
  OSType newState;
  switch ([sender state]) {
    case kStatzStateAway:
      newState = 'awaY';
      break;

    case kStatzStateInvisible:
      newState = 'aSaI';
      break;
      
    case kStatzStateOffline:
      newState = 'offL';
      break;
      
    default:
    case kStatzStateAvailable:
      newState = 'avaL';
      break;
  }
  
  NSAppleEventDescriptor *descriptor = [AEVT class:kAECoreSuite 
                                                id:kAEGetData
                                    targetBundleID:[self appBundleID],
    [KEY : keyAEData], [STRING : newValue],
    [KEY : keyDirectObject],
    [RECORD : cObjectSpecifier,
      [KEY : keyAEKeyForm], [ENUM : formPropertyID],
      [KEY : keyAEDesiredClass], [TYPE : cProperty],
      [KEY : keyAEKeyData], [TYPE : 'Amms'],
      [KEY : keyAEContainer], [DESC null],
      ENDRECORD],
    ENDRECORD];
  require_ae_noerr(descriptor, cantUpdate);
  NSAppleEventDescriptor *reply = [descriptor sendWithImmediateReply];
  require_ae_noerr(reply, cantUpdate);
  NSAppleEventDescriptor *keyDirectDesc = [reply descriptorForKeyword:keyDirectObject];
  NSString *oldStatus = @"";
  // Error in Adium 1.1.4 where it doesn't always return a status message.
  if (keyDirectDesc) {
    require_ae_noerr(keyDirectDesc, cantUpdate);
    oldStatus = [keyDirectDesc stringValue];
  }
  
  descriptor = [AEVT class:kAECoreSuite 
                        id:kAEGetData
            targetBundleID:[self appBundleID],
    [KEY : keyAEData], [STRING : newValue],
    [KEY : keyDirectObject],
    [RECORD : cObjectSpecifier,
      [KEY : keyAEKeyForm], [ENUM : formPropertyID],
      [KEY : keyAEDesiredClass], [TYPE : cProperty],
      [KEY : keyAEKeyData], [TYPE : 'AmSy'],
      [KEY : keyAEContainer], [DESC null],
      ENDRECORD],
    ENDRECORD];
  require_ae_noerr(descriptor, cantUpdate);
  reply = [descriptor sendWithImmediateReply];
  require_ae_noerr(reply, cantUpdate);
  keyDirectDesc = [reply descriptorForKeyword:keyDirectObject];
  require_ae_noerr(keyDirectDesc, cantUpdate);  
  OSType oldState = [keyDirectDesc enumCodeValue];
  if ([oldStatus isEqualTo:newValue] && oldState == newState) {
    return kStatzUpdateNotChanged;
  }
  
  descriptor = [AEVT class:kAECoreSuite 
                        id:kAESetData
            targetBundleID:[self appBundleID],
                [KEY : keyAEData], [ENUM : newState],
                [KEY : keyDirectObject],
                [RECORD : cObjectSpecifier,
                 [KEY : keyAEKeyForm], [ENUM : formPropertyID],
                 [KEY : keyAEDesiredClass], [TYPE : cProperty],
                 [KEY : keyAEKeyData], [TYPE : 'AmSy'],
                 [KEY : keyAEContainer], [DESC null],
                 ENDRECORD],
                ENDRECORD];
  require_ae_noerr(descriptor, cantUpdate);
  reply = [descriptor sendWithImmediateReply];
  require_ae_noerr(reply, cantUpdate);

  descriptor = [AEVT class:kAECoreSuite 
                        id:kAESetData
            targetBundleID:[self appBundleID],
    [KEY : keyAEData], [STRING : newValue],
    [KEY : keyDirectObject],
    [RECORD : cObjectSpecifier,
      [KEY : keyAEKeyForm], [ENUM : formPropertyID],
      [KEY : keyAEDesiredClass], [TYPE : cProperty],
      [KEY : keyAEKeyData], [TYPE : 'Amms'],
      [KEY : keyAEContainer], [DESC null],
      ENDRECORD],
    ENDRECORD];
  require_ae_noerr(descriptor, cantUpdate);
  reply = [descriptor sendWithImmediateReply];
  require_ae_noerr(reply, cantUpdate);

  return kStatzUpdateSuccess;
cantUpdate:
  return kStatzUpdateError;
}

- (BOOL)appHasStatusMenuItem {
  BOOL hasItem = NO;
  NSArray *appSupportDirs = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, 
                                                                NSUserDomainMask, 
                                                                YES);
  if ([appSupportDirs count]) {
    NSArray *components = [NSArray arrayWithObjects:
      [appSupportDirs objectAtIndex:0], 
      @"Adium 2.0", 
      @"Users", 
      @"Default", 
      @"Status Menu Item.plist", 
      nil];
    NSString *adiumPlistPath = [NSString pathWithComponents:components];
    NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:adiumPlistPath];
    NSNumber *enabledNum = [prefs objectForKey:@"Status Menu Item Enabled"];
    hasItem = [enabledNum boolValue];
  }
  return hasItem;
}

- (Status*)currentStatus {
  return nil;
}

- (unsigned int)stateMenuMask { 
  return kStatzStateAvailable 
  | kStatzStateAway 
  | kStatzStateInvisible 
  | kStatzStateOffline;
}
@end
